package com.epay;

import java.io.File;
import java.io.FileWriter;

public class CreaterController {

	public static boolean createrController(){
		try{
			String workpath = Constants.workpath;
			String tableNameLower=Constants.fileName;
			String tableName=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1);
			String thisService=Constants.fileName+"Service";

			//创建conttroller文件
			String dir = workpath + File.separator + CreaterUtil.replaceAll(Constants.struts_action_pack, ".", File.separator);
			File dirfile = new File(dir);
			if (!dirfile.exists()) {
			   dirfile.mkdirs();
			}
			File beanfile = new File(dir+"/" + tableName+"Controller.java");
			FileWriter fw2 = new FileWriter(beanfile);
			fw2.write("package " + Constants.struts_action_pack + ";\n");
			fw2.write("import javax.annotation.Resource;\n");
			fw2.write("import "+Constants.business_package+"."+tableName+"Service;\n");
			fw2.write("import "+Constants.searchModel_package+"."+tableName+"DO;\n");
			fw2.write("import "+Constants.struts_dto_pack+".request."+tableName+"ReqDTO;\n");
			fw2.write("import "+Constants.struts_dto_pack+".response."+tableName+"ResDTO;\n");
			fw2.write("import "+Constants.struts_dto_pack+".response."+tableName+"ListResDTO;\n");

			fw2.write("import org.slf4j.Logger;\n");
			fw2.write("import org.slf4j.LoggerFactory;\n");
			fw2.write("import org.springframework.web.bind.annotation.*;\n");
			fw2.write("import com.epay.common.response.BaseResult;\n");
			fw2.write("import com.github.pagehelper.PageInfo;");
			fw2.write("import com.epay.common.conversion.ObjectConversion;");
			fw2.write("import com.epay.common.page.Page;\n");

			fw2.write("\n");
			 
			 fw2.write("/**\n");
			 fw2.write(" * contorller\n");
			 fw2.write(" * @author Epay\n");
			 fw2.write(" * @date "+Constants.getSysDate()+"\n");
			 fw2.write(" */\n");
			 fw2.write("@RestController\n");
			 fw2.write("@RequestMapping(value=\"/"+tableNameLower+"\")\n");
			 fw2.write("public class " + tableName + "Controller{");
			 fw2.write("\n");

			 fw2.write("private static final Logger logger = LoggerFactory.getLogger("+tableName+"Controller.class);\n");
			 fw2.write("\t@Resource\n");
			 fw2.write("\tprivate "+tableName+"Service "+thisService+";\n");
			 fw2.write("\t@Resource\n");
			 fw2.write("\tprivate ObjectConversion objectConversion;\n");

			 fw2.write("\t/**\n");
			 fw2.write("\t* 查询\n");
			 fw2.write("\t* @param "+tableNameLower+"ReqDTO\n");
			 fw2.write("\t* @return "+tableName+"ListResDTO\n");
			 fw2.write("\t*/\n");
			 fw2.write("\t@GetMapping(value = \"/list\")\n");
			 fw2.write("\tpublic BaseResult<"+tableName+"ListResDTO> list("+tableName+"ReqDTO "+tableNameLower+"ReqDTO) {\n");
			 fw2.write("\t\t"+tableName+"ListResDTO "+tableNameLower+"ListResDTO = new "+tableName+"ListResDTO();\n");

			 fw2.write("\t\tBaseResult<"+tableName+"ListResDTO> result = new BaseResult<>();  \n");
			 fw2.write("\t\t"+tableName+"DO "+tableNameLower+"DO = objectConversion.copy("+tableNameLower+"ReqDTO,"+tableName+"DO.class);\n");
			 fw2.write("\t\tPageInfo<"+tableName+"DO> pageInfo = objectConversion.copy("+tableNameLower+"ReqDTO,PageInfo.class);\n");
			 fw2.write("\t\tpageInfo="+thisService+".getPageList("+tableNameLower+"DO,pageInfo);\n");

			 fw2.write("\t\t"+tableNameLower+"ListResDTO.setList(objectConversion.copy(pageInfo.getList(),"+tableName+"ResDTO.class));\n");
			 fw2.write("\t\t"+tableNameLower+"ListResDTO.setPage(objectConversion.copy(pageInfo,Page.class));\n");
			 fw2.write("\t\tresult.setData("+tableNameLower+"ListResDTO);\n");
			 fw2.write("\t\treturn result;\n");
			 fw2.write("\t}\n");


			fw2.write("\t/**\n");
			fw2.write("\t* 获取单个\n");
			fw2.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw2.write("\t* @return "+tableName+"ResDTO\n");
			fw2.write("\t*/\n");
			fw2.write("\t@GetMapping(value = \"/get\")\n");
			fw2.write("\tpublic BaseResult<"+tableName+"ResDTO> get("+tableName+"ReqDTO "+tableNameLower+"ReqDTO) {\n");
			fw2.write("\t\tBaseResult<"+tableName+"ResDTO> result = new BaseResult<>();  \n");
			fw2.write("\t\t"+tableName+"DO "+tableNameLower+"DO = objectConversion.copy("+tableNameLower+"ReqDTO,"+tableName+"DO.class);\n");
			fw2.write("\t\t"+tableNameLower+"DO = "+thisService+".get("+tableNameLower+"DO);\n");
			fw2.write("\t\t"+tableName+"ResDTO "+tableNameLower+"ResDTO =objectConversion.copy("+tableNameLower+"DO,"+tableName+"ResDTO.class);\n");
			fw2.write("\t\tresult.setData("+tableNameLower+"ResDTO);\n");
			fw2.write("\t\treturn result;\n");
			fw2.write("\t}\n");

			fw2.write("\t/**\n");
			fw2.write("\t* 修改\n");
			fw2.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw2.write("\t* @return "+tableName+"ResDTO\n");
			fw2.write("\t*/\n");
			fw2.write("\t@PostMapping(value = \"/update\")\n");
			fw2.write("\tpublic BaseResult<"+tableName+"ResDTO> update(@RequestBody "+tableName+"ReqDTO "+tableNameLower+"ReqDTO) {\n");
			fw2.write("\t\tBaseResult<"+tableName+"ResDTO> result = new BaseResult<>();  \n");
			fw2.write("\t\t"+tableName+"DO "+tableNameLower+"DO = objectConversion.copy("+tableNameLower+"ReqDTO,"+tableName+"DO.class);\n");
			fw2.write("\t\t"+thisService+".update("+tableNameLower+"DO);\n");
			fw2.write("\t\t"+tableName+"ResDTO "+tableNameLower+"ResDTO =objectConversion.copy("+tableNameLower+"DO,"+tableName+"ResDTO.class);\n");
			fw2.write("\t\tresult.setData("+tableNameLower+"ResDTO);\n");
			fw2.write("\t\treturn result;\n");
			fw2.write("\t}\n");


			fw2.write("\t/**\n");
			fw2.write("\t* 增加\n");
			fw2.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw2.write("\t* @return "+tableName+"ResDTO\n");
			fw2.write("\t*/\n");
			fw2.write("\t@PostMapping(value = \"/insert\")\n");
			fw2.write("\tpublic BaseResult<"+tableName+"ResDTO> insert(@RequestBody "+tableName+"ReqDTO "+tableNameLower+"ReqDTO) {\n");
			fw2.write("\t\tBaseResult<"+tableName+"ResDTO> result = new BaseResult<>();  \n");
			fw2.write("\t\t"+tableName+"DO "+tableNameLower+"DO = objectConversion.copy("+tableNameLower+"ReqDTO,"+tableName+"DO.class);\n");
			fw2.write("\t\t"+thisService+".insert("+tableNameLower+"DO);\n");
			fw2.write("\t\t"+tableName+"ResDTO "+tableNameLower+"ResDTO =objectConversion.copy("+tableNameLower+"DO,"+tableName+"ResDTO.class);\n");
			fw2.write("\t\tresult.setData("+tableNameLower+"ResDTO);\n");
			fw2.write("\t\treturn result;\n");
			fw2.write("\t}\n");
			fw2.write("}\n");
			fw2.close();


			//创建conttroller文件
			String dir1 = workpath + File.separator + CreaterUtil.replaceAll(Constants.struts_dto_pack, ".", File.separator);
			File dirfile1 = new File(dir1);
			if (!dirfile1.exists()) {
				dirfile1.mkdirs();
			}
			File beanfile1 = new File(dir1+"/" + tableName+"Client.java");
			FileWriter fw1 = new FileWriter(beanfile1);
			fw1.write("package " + Constants.struts_action_pack + ";\n");
			fw1.write("import "+Constants.struts_dto_pack+".request."+tableName+"ReqDTO;\n");
			fw1.write("import "+Constants.struts_dto_pack+".response."+tableName+"ResDTO;\n");
			fw1.write("import "+Constants.struts_dto_pack+".response."+tableName+"ListResDTO;\n");

			fw1.write("import org.springframework.cloud.openfeign.FeignClient;\n");
			fw1.write("import feign.hystrix.FallbackFactory;\n");
			fw1.write("import org.springframework.web.bind.annotation.*;\n");
			fw1.write("import com.epay.common.response.BaseResult;\n");
			fw1.write("\n");

			fw1.write("/**\n");
			fw1.write(" * contorller\n");
			fw1.write(" * @author Epay\n");
			fw1.write(" * @date "+Constants.getSysDate()+"\n");
			fw1.write(" */\n");
			fw1.write("@FeignClient(name = \"management\",contextId = \""+tableName+"Client\",fallback = FallbackFactory.Default.class)\n");
			fw1.write("public interface " + tableName + "Client{");
			fw1.write("\n");

			fw1.write("\t/**\n");
			fw1.write("\t* 查询\n");
			fw1.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw1.write("\t* @return "+tableName+"ListResDTO\n");
			fw1.write("\t*/\n");
			fw1.write("\t@GetMapping(value = \"/"+tableNameLower+"/list\")\n");
			fw1.write("\tBaseResult<"+tableName+"ListResDTO> list("+tableName+"ReqDTO "+tableNameLower+"ReqDTO);\n");


			fw1.write("\t/**\n");
			fw1.write("\t* 获取单个\n");
			fw1.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw1.write("\t* @return "+tableName+"ResDTO\n");
			fw1.write("\t*/\n");
			fw1.write("\t@GetMapping(value = \"/"+tableNameLower+"/get\")\n");
			fw1.write("\tBaseResult<"+tableName+"ResDTO> get("+tableName+"ReqDTO "+tableNameLower+"ReqDTO);\n");

			fw1.write("\t/**\n");
			fw1.write("\t* 修改\n");
			fw1.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw1.write("\t* @return "+tableName+"ResDTO\n");
			fw1.write("\t*/\n");
			fw1.write("\t@PostMapping(value = \"/"+tableNameLower+"/update\")\n");
			fw1.write("\tBaseResult<"+tableName+"ResDTO> update(@RequestBody "+tableName+"ReqDTO "+tableNameLower+"ReqDTO);\n");

			fw1.write("\t/**\n");
			fw1.write("\t* 增加\n");
			fw1.write("\t* @param "+tableNameLower+"ReqDTO\n");
			fw1.write("\t* @return "+tableName+"ResDTO\n");
			fw1.write("\t*/\n");
			fw1.write("\t@PostMapping(value = \"/"+tableNameLower+"/insert\")\n");
			fw1.write("\tBaseResult<"+tableName+"ResDTO> insert(@RequestBody "+tableName+"ReqDTO "+tableNameLower+"ReqDTO);\n");
			fw1.write("}\n");
			fw1.close();
		}catch(Exception e){
			System.out.println("创建controller异常");
			e.printStackTrace();
		}
		return true;
	}
}
